package demo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class Form extends JFrame {
	
	private JButton addButton;
	private JButton retrieveButton;
	private JTextField companyKeyTextField;
	private JTextField companyNameTextField;
	private JTextField companyAddressTextField;
	private JLabel companyKeyLabel;
	private JLabel companyNameLabel;
	private JLabel companyAddressLabel;
	private JTextArea companyListTextArea;
	private JScrollPane companyListScrollPane;
	
	public Form() {
		// Create button
		addButton = new JButton("Add company");
		addButton.setSize(130, 40);
		addButton.setLocation(120, 120);
		addButton.addActionListener(addButtonActionListener);
		
		retrieveButton = new JButton("Retrieve");
		retrieveButton.setSize(130, 40);
		retrieveButton.setLocation(250, 120);
		retrieveButton.addActionListener(retrieveButtonActionListener);
		
		// Create company key label
		companyKeyLabel = new JLabel("Company key");
		companyKeyLabel.setSize(200, 20);
		companyKeyLabel.setLocation(10, 20);
		
		// Create company key text field
		companyKeyTextField = new JTextField();
		companyKeyTextField.setSize(200, 20);
		companyKeyTextField.setLocation(150, 20);
		
		// Create company name text field
		companyNameTextField = new JTextField();
		companyNameTextField.setSize(200, 20);
		companyNameTextField.setLocation(150, 50);
		
		// Create company address text field
		companyAddressTextField = new JTextField();
		companyAddressTextField.setSize(200, 20);
		companyAddressTextField.setLocation(150, 80);
		
		// Create company name label
		companyNameLabel = new JLabel("Company name");
		companyNameLabel.setSize(200, 20);
		companyNameLabel.setLocation(10, 50);
		
		// Create company address label
		companyAddressLabel = new JLabel("Company address");
		companyAddressLabel.setSize(200, 20);
		companyAddressLabel.setLocation(10, 80);
		
		// Create company list text area
		companyListTextArea = new JTextArea();
		companyListTextArea.setEditable(false);
		companyListTextArea.setSize(400, 100);
		companyListTextArea.setLocation(0, 165);
		
		companyListScrollPane = new JScrollPane(companyListTextArea);
		companyListScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		// Initialize form
		this.setTitle("Form");
		this.setSize(400, 300);
		this.setLayout(null);
		this.add(addButton);
		this.add(retrieveButton);
		this.add(companyNameLabel);
		this.add(companyAddressLabel);
		this.add(companyKeyLabel);
		this.add(companyNameTextField);
		this.add(companyAddressTextField);
		this.add(companyKeyTextField);
		this.add(companyListTextArea);
	}
	
	private CompanyBO companyBO = new CompanyBO();
	
	private ActionListener addButtonActionListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			companyBO.addCompany(companyKeyTextField.getText(), companyNameTextField.getText(), companyAddressTextField.getText());
			JOptionPane.showMessageDialog(null, "Success", "OK", JOptionPane.DEFAULT_OPTION);
		}
	};
	
	private ActionListener retrieveButtonActionListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			List<Company> companies = companyBO.retrieveCompanies();
			StringBuilder builder = new StringBuilder();
			
			for (Company c : companies) {
				builder.append(String.format("%-10s%-10s%-10s", c.getCompanyKey(), c.getCompanyName(), c.getCompanyAddress()));
				builder.append("\n");
			}
			
			companyListTextArea.setText(builder.toString());
		}
	};
}
